package com.mmbang.util;


import java.math.BigDecimal;
import java.util.Random;


import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringOperation {

	
//	 判断是否数值的函数
    public static boolean isNumeric(String inputstr)
    {
        boolean rv=true;
        if(inputstr!=null)
        {
            byte[] bytearray=inputstr.getBytes();
            int bytelength=bytearray.length;
            int i=0;
            while(i<bytelength&&rv)
            {
                if(bytearray[i]<48||bytearray[i]>57)
                {
                  rv=false;
                }
                i=i+1;
            }
        }
        else
        {
            rv=false;
        }
        return rv;
    }
	
    public static String getDisplayString(String strValue) {
    	String returnValue=strValue;
    	if(strValue == null) {
    		returnValue= "";
    	}else {
    		if(strValue.equalsIgnoreCase("null")) {
    			returnValue= "";
    		}
    	}
    	return returnValue;
    }
    
	/**  
     * 删除字符串中的html格式  
     *   
     * @param input  
     * @param length  
     * @return  
     */  
    public static String splitAndFilterString(String input, int length) {   
        if (input == null || input.trim().equals("")) {   
            return "";   
        }   
        // 去掉所有html元素,   
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(   
                "<[^>]*>", "");   
        str = str.replaceAll("[(/>)<]", "");
        str = str.replaceAll("\\n", "");
        str = str.replaceAll("   ", "");
        str = str.replaceAll("	", "");
        str = str.replaceAll("\"", "");
        //搜索转义处理
        str = str.replaceAll("\\\\", "/");
        str = str.replace("\n", "");
        int len = str.length();   
        if (len <= length) {   
            return str;   
        } else {   
            str = str.substring(0, length);   
            str += "......";   
        }   
        return str;   
    }
    
    
    //特殊字符处理
    public static String splitDiffString(String strValue) {
    	String returnValue = "";
    	returnValue = strValue.replaceAll("&nbsp", "");
    	return returnValue;
    }
    
    //对mangodb数据库里字段带"."的数字进行处理
    //对科学技术法的处理
    public static String getNumberString(String StringNumber) {
		String returnNumber = StringNumber;
		if(StringNumber.indexOf(".")>=0 && StringNumber.indexOf("E")<0) {
			returnNumber = StringNumber.substring(0,StringNumber.indexOf("."));
		}
		if(StringNumber.indexOf(".")>=0 && StringNumber.indexOf("E")>0) {
			BigDecimal bd = new BigDecimal(StringNumber);  
//			double num1 = Double.parseDouble(StringNumber.substring(0,StringNumber.indexOf("E")));
//			double num2 = Double.parseDouble(StringNumber.substring(StringNumber.indexOf("E")+1));
			returnNumber = bd.toPlainString() ;
		}		
		return returnNumber;
    }
    
    
    //处理json异常字符
    static StringBuffer string2Json(String s) {     
        StringBuffer sb = new StringBuffer ();     
        for (int i=0; i<s.length(); i++) {     
       
            char c = s.charAt(i);     
            switch (c) {     
            case '\"':     
                sb.append("\\\"");     
                break;     
            case '\\':     
                sb.append("\\\\");     
//            	sb.append("/");
                break;     
            case '/':     
                sb.append("\\/");     
                break;     
            case '\b':     
                sb.append("\\b");     
                break;     
            case '\f':     
                sb.append("\\f");     
                break;     
            case '\n':     
                sb.append("\\n");     
                break;     
            case '\r':     
                sb.append("\\r");     
                break;     
            case '\t':     
                sb.append("\\t");     
                break;     
            default:     
                sb.append(c);
            }
        }
        return sb;     
     } 
    
	 // <summary>
	  /// Json特符字符过滤，参见http://www.json.org/
	  /// </summary>
	  /// <param name="sourceStr">要过滤的源字符串</param>
	  /// <returns>返回过滤的字符串</returns>
	  public static String JsonCharFilter(String sourceStr)
	  {
	      sourceStr = sourceStr.replace("\\", "\\\\");
		  sourceStr = sourceStr.replace("\b", "\\\b");
		  sourceStr = sourceStr.replace("\t", "\\\t");
		  sourceStr = sourceStr.replace("\n", "\\\n");
		  sourceStr = sourceStr.replace("\n", "\\\n");
		  sourceStr = sourceStr.replace("\f", "\\\f");
		  sourceStr = sourceStr.replace("\r", "\\\r");
//		  sourceStr = sourceStr.replace("s", "hhhhh");
		  return sourceStr.replace("\"", "\\\"");
	  }
	    
	  
	  
	   public static String escapeStr(String input) {   
	        if (input == null || input.trim().equals("")) {   
	            return "";   
	        }   
	        // 去掉所有html元素,   
	        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(   
	                "<[^>]*>", "");   
	        str = str.replaceAll("[(/>)<]", "");
	        str = str.replaceAll("\\n", "");
	        str = str.replaceAll("?", "");
	        str = str.replaceAll("*", "");
	        str = str.replaceAll("%", "");
	        str = str.replace("\n", "");  
	        return str;   
	    }	  
	  
	   /**
	     * 转义Solr/Lucene的保留运算字符
	     * 保留字符有+ - && || ! ( ) { } [ ] ^ ” ~ * ? : \
	     * @param input
	     * @return 转义后的字符串
	     */
	    public static String transformSolrMetacharactor(String input){
	        StringBuffer sb = new StringBuffer();
	        String regex = "[+\\-&|!(){}\\[\\]^\"~*?:(\\)]";
	        Pattern pattern = Pattern.compile(regex);
	        Matcher matcher = pattern.matcher(input);
	        while(matcher.find()){
	            matcher.appendReplacement(sb, "\\\\"+matcher.group());
	        }
	        matcher.appendTail(sb);
	        return sb.toString();
	    }
	  
	  
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringOperation stringOperation = new StringOperation();
		
		
		//System.out.println(isNumeric("1111"));
//		System.out.println(getDisplayString("null").equals(""));
//		System.out.println(getDisplayString("").equals(""));
//		System.out.println(getDisplayString("NULL").equals(""));
//		System.out.println(getDisplayString(null).equals(""));
//		String strTest = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp哈哈";
//		strTest = "haha";
//		System.out.println(stringOperation.splitDiffString(strTest));
//		
		String tmp = "来6月报个道，告诉大家，想你们了！<img src='http://www.mmbang.info/smiles/qq.gif' class='smile' />道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想道，告诉大家，想";
		
		tmp = "又到岁末，2011年因为有宝贝在身边，我们的生活每天都过得忙碌和幸福~~	各位麻麻们，你家宝贝出生后......";
		tmp ="今天很早s就去s医院检s查啦，\"打B超，\"听胎心：\"BC结果：双顶径2.7cm；胎心率140次\\分钟；股骨长\\径1......";
//		System.out.println(stringOperation.splitAndFilterString(tmp, 50));

		//3.2508973E7
		
		//System.out.println(getNumberString(getNumberString("1.370318412356E9")));
		//System.out.println(getNumberString(getNumberString("13703184123569")));
		
//		System.out.println(splitAndFilterString(tmp,200));
//		
//		System.out.println(stringOperation.string2Json(tmp));
//		
//		System.out.println(stringOperation.JsonCharFilter(tmp));
//		
//		
//		System.out.println(escapeStr("%%%%###$$$*****((()))hhaha"));
		
		
		
		System.out.println(getNumberString("806352.0"));
		
		
		
	}

}
